<template>
  <el-container class="frame" style="height: 100%">
    <!-- 侧边栏 -->
    <el-aside :width="isMobile? '0' : (isCollapse? '50px' : '200px')" class="my-aside">
      <div class="aside-header">
        <img src="@/assets/images/scau-logo.png" alt="Logo" class="logo-img" />
        <h5 v-if="!isCollapse && !isMobile" class="logo">SCAU - 作业管理系统</h5>
      </div>
      <el-menu
          :collapse="isCollapse"
          class="menu-style"
          default-active="admin-home"
          @select="pageChange"
      >
        <template v-if="!isCollapse || isMobile">
          <el-menu-item class="side-menu-item" index="class-management" @click="pageChange('/classManagement')">
            班级管理
          </el-menu-item>
          <el-menu-item class="side-menu-item" index="course-management" @click="pageChange('/courseManagement')">
            课程管理
          </el-menu-item>
          <el-menu-item class="side-menu-item" index="students-management" @click="pageChange('/studentManagement')">
            学生管理
          </el-menu-item>
          <el-menu-item class="side-menu-item" index="teachers-management" @click="pageChange('/teacherManagement')">
            教师管理
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    <el-container style="width: 100%">
      <!-- 顶部栏 -->
      <el-header class="my-top-nav" :style="{ left: headerLeft }">
        <div style="display: flex; align-items: center;">
          <el-button text @click="toggleMenu">
            <el-icon>
              <component :is="isCollapse? Expand : Fold" />
            </el-icon>
            <h5 style="margin-left: 10px">管理员端</h5>
          </el-button>
        </div>
        <div class="user-info">
          <span style="margin-right: 10px;">欢迎你,管理员</span>
          <el-button type="info" round @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>
      <!-- 主体内容 -->
      <el-main class="main-content" :style="{ marginLeft: mainContentMarginLeft }">
        <router-view class="content-area" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { Expand, Fold, Menu } from "@element-plus/icons-vue";
import { computed, onMounted, ref } from "vue";
import { useRouter } from "vue-router";

export default {
  name: "AdminHome",
  computed: {
    mainContentMarginLeft() {
      return this.isMobile? '0px' : (this.isCollapse? '50px' : '200px');
    }
  },
  components: {
    [Menu.name]: Menu,
    [Fold.name]: Fold,
    [Expand.name]: Expand,
  },
  setup() {
    const isCollapse = ref(false);
    const isMobile = ref(false);
    const router = useRouter();

    const checkScreen = () => {
      isMobile.value = window.innerWidth < 768;
    };

    const toggleMenu = () => {
      isCollapse.value = !isCollapse.value;
    };

    const headerLeft = computed(() => {
      if (isMobile.value) return '0px';
      return isCollapse.value? '50px' : '200px';
    });

    onMounted(() => {
      checkScreen();
      window.addEventListener('resize', checkScreen);
    });

    return {
      isCollapse,
      isMobile,
      toggleMenu,
      headerLeft,
      pageChange(url) {
        router.push(url);
      },
      handleLogout() {
        localStorage.removeItem('adminId');
        router.push('/');
      },
    };
  },
  methods: {},
};
</script>

<style scoped>
/* 添加的CSS样式 */
.el-main.main-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  transition: margin-left 0.3s;
}

.logo {
  color: #e6f5e9;
  text-align: center;
}

.side-menu-item {
  background-color: #4d6652; /* 侧边栏菜单项背景色 - 深灰绿色 */
  color: #e6f5e9; /* 浅绿色文字 */
}

/* 浅灰色箭头 */
::v-deep(.el-sub-menu__icon-arrow) {
  color: #c7d9c9!important;
}

/* 悬停/激活状态背景色改为橄榄绿，文字保持浅色 */
::v-deep(.el-sub-menu__title:hover),
::v-deep(.el-menu-item:hover),
.el-menu-item.is-active,
.el-sub-menu.is-active > .el-sub-menu__title {
  background-color: #6b8e71 !important; /* 中灰绿色背景 */
  color: #f7fcf5!important; /* 接近白色的文字 */
}

.my-aside {
  height: 100vh;
  background-color: #384d3c; /* 侧边栏整体背景色 - 低饱和度深绿色 */
  overflow: hidden;
  transition: width 0.3s;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  display: flex;
  flex-direction: column;
}

.aside-header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  flex-shrink: 0;
  background-color: #28372a; /* 侧边栏头部背景色 - 更深的绿色 */
}

.logo-img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #e6f5e9;
  margin-right: 10px;
}

.my-top-nav {
  position: fixed;
  top: 0;
  right: 0;
  height: 50px;
  padding: 0 20px;
  background-color: #f7fcf5;
  border-bottom: 1px solid #e5f5e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
  transition: left 0.3s ease;
}

.content-area {
  background-color: #f1f7ee;
  padding: 0;
  height: auto;
  overflow-y: auto;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  width: 100%;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .main-content,
  .my-aside {
    width: 0!important;
  }
  .logo {
    display: none;
  }
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.menu-style {
  background-color: #384d3c;
  border-right: none;
  color: #e6f5e9;
}

.el-menu-item {
  color: #e6f5e9;
}

::v-deep(.el-container.is-vertical){
  background-color: rgba(215, 232, 218, 0.73);
}
</style>